<?php require_once('../Connections/learnConn.php'); ?>
<?php
if (!isset($_SESSION)) {
  session_start();
}
$MM_authorizedUsers = "guru";
$MM_donotCheckaccess = "false";

// *** Restrict Access To Page: Grant or deny access to this page
function isAuthorized($strUsers, $strGroups, $UserName, $UserGroup) { 
  // For security, start by assuming the visitor is NOT authorized. 
  $isValid = False; 

  // When a visitor has logged into this site, the Session variable MM_Username set equal to their username. 
  // Therefore, we know that a user is NOT logged in if that Session variable is blank. 
  if (!empty($UserName)) { 
    // Besides being logged in, you may restrict access to only certain users based on an ID established when they login. 
    // Parse the strings into arrays. 
    $arrUsers = Explode(",", $strUsers); 
    $arrGroups = Explode(",", $strGroups); 
    if (in_array($UserName, $arrUsers)) { 
      $isValid = true; 
    } 
    // Or, you may restrict access to only certain users based on their username. 
    if (in_array($UserGroup, $arrGroups)) { 
      $isValid = true; 
    } 
    if (($strUsers == "") && false) { 
      $isValid = true; 
    } 
  } 
  return $isValid; 
}

$MM_restrictGoTo = "../login.php";
if (!((isset($_SESSION['MM_Username'])) && (isAuthorized("",$MM_authorizedUsers, $_SESSION['MM_Username'], $_SESSION['MM_UserGroup'])))) {   
  $MM_qsChar = "?";
  $MM_referrer = $_SERVER['PHP_SELF'];
  if (strpos($MM_restrictGoTo, "?")) $MM_qsChar = "&";
  if (isset($QUERY_STRING) && strlen($QUERY_STRING) > 0) 
  $MM_referrer .= "?" . $QUERY_STRING;
  $MM_restrictGoTo = $MM_restrictGoTo. $MM_qsChar . "accesscheck=" . urlencode($MM_referrer);
  header("Location: ". $MM_restrictGoTo); 
  exit;
}
?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  if (PHP_VERSION < 6) {
    $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  }

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
  $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}

if ((isset($_POST["MM_update"])) && ($_POST["MM_update"] == "form1")) {
  $updateSQL = sprintf("UPDATE nilai SET ul1=%s, ul2=%s, ul3=%s, ul4=%s, ul5=%s, tgs1=%s, tgs2=%s, tgs3=%s, tgs4=%s, tgs5=%s, uts=%s, uas=%s, nilai_akhir=%s WHERE nis=%s AND kd_kelas=%s AND kd_kat_kelas=%s AND kd_matpel=%s AND tahun_ajaran=%s AND semester=%s",
                       
                       GetSQLValueString($_POST['ul1'], "float"),
                       GetSQLValueString($_POST['ul2'], "float"),
                       GetSQLValueString($_POST['ul3'], "float"),
                       GetSQLValueString($_POST['ul4'], "float"),
                       GetSQLValueString($_POST['ul5'], "float"),
                       GetSQLValueString($_POST['tgs1'], "float"),
                       GetSQLValueString($_POST['tgs2'], "float"),
                       GetSQLValueString($_POST['tgs3'], "float"),
                       GetSQLValueString($_POST['tgs4'], "float"),
                       GetSQLValueString($_POST['tgs5'], "float"),
                       GetSQLValueString($_POST['uts'], "float"),
                       GetSQLValueString($_POST['uas'], "float"),
                       GetSQLValueString($_POST['nilai_akhir'], "float"),
                       GetSQLValueString($_POST['nis'], "int"),
					   GetSQLValueString($_GET['k'], "text"),
                       GetSQLValueString($_GET['ks'], "text"),
                       GetSQLValueString($_GET['kp'], "text"),
                       GetSQLValueString($_POST['tahun_ajaran'], "text"),
                       GetSQLValueString($_POST['semester'], "text"));

  mysql_select_db($database_learnConn, $learnConn);
  $Result1 = mysql_query($updateSQL, $learnConn) or die(mysql_error());

  $kp = $_GET['kp'];
  $k = $_GET['k'];
  $ks = $_GET['ks'];
  $updateGoTo = "list_siswa.php?kode_matpel=$kp&kode_kelas=$k&kode_sub_kelas=$ks";
  if (isset($_SERVER['QUERY_STRING'])) {
    $updateGoTo .= (strpos($updateGoTo, '?')) ? "&" : "?";
    $updateGoTo .= $_SERVER['QUERY_STRING'];
  }
  header(sprintf("Location: %s", $updateGoTo));
}

$colname_nilaiE = "-1";
if (isset($_GET['nis'])) {
  $colname_nilaiE = $_GET['nis'];
}
mysql_select_db($database_learnConn, $learnConn);
$query_nilaiE = sprintf("SELECT * FROM nilai WHERE nis = %s AND kd_kelas=%s AND kd_kat_kelas=%s AND kd_matpel=%s", 
						GetSQLValueString($colname_nilaiE, "int"),
					   GetSQLValueString($_GET['k'], "text"),
                       GetSQLValueString($_GET['ks'], "text"),
                       GetSQLValueString($_GET['kp'], "text"));
$nilaiE = mysql_query($query_nilaiE, $learnConn) or die(mysql_error());
$row_nilaiE = mysql_fetch_assoc($nilaiE);
$totalRows_nilaiE = mysql_num_rows($nilaiE);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Edit Nilai</title>
<link href="../style/style.css" type="text/css" rel="stylesheet" />
<script language="javascript" type="text/javascript">
function hitungNilai() {
	 var ul1 = parseInt(document.form1.ul1.value);
	 var ul2 = parseInt(document.form1.ul2.value);
	 var ul3 = parseInt(document.form1.ul3.value);
	 var ul4 = parseInt(document.form1.ul4.value);
	 var ul5 = parseInt(document.form1.ul5.value);
	 
	 var tgs1 = parseInt(document.form1.tgs1.value);
	 var tgs2 = parseInt(document.form1.tgs2.value);
	 var tgs3 = parseInt(document.form1.tgs3.value);
	 var tgs4 = parseInt(document.form1.tgs4.value);
	 var tgs5 = parseInt(document.form1.tgs5.value);
	 
	 var uts = parseInt(document.form1.uts.value);
	 var uas = parseInt(document.form1.uas.value);
	 
	 if (ul1 != 0 && ul2 !=0 && ul3 != 0 && ul4 !=0 && ul5 !=0) {
		 var i = 5;
	 }
	 else if (ul1 != 0 && ul2 !=0 && ul3 != 0 && ul4 !=0 && ul5 ==0) {
		 var i = 4;
	 }
	 
	 else if (ul1 != 0 && ul2 !=0 && ul3 != 0 && ul4 ==0 && ul5 ==0) {
		 var i = 3;
	 }
	 
	 else if (ul1 != 0 && ul2 !=0 && ul3 == 0 && ul4 ==0 && ul5 ==0) {
		 var i = 2;
	 }
	 
	 else if (ul1 != 0 && ul2 ==0 && ul3 == 0 && ul4 ==0 && ul5 ==0) {
		 var i = 1;
	 }
	 
	 var totUl = (ul1 + ul2 + ul3 + ul4 + ul5) / i;
	 var totUl2 = totUl * 0.6;
	 var totUts = uts * 0.2;
	 var totUas = uas * 0.2;
	 
	 var totAll = totUl2 + totUts + totUas ;
	 var aaa = totAll.toFixed(2);
	 document.form1.nilai_akhir.value = aaa;
 }
function MM_validateForm() { //v4.0
  if (document.getElementById){
    var i,p,q,nm,test,num,min,max,errors='',args=MM_validateForm.arguments;
    for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=document.getElementById(args[i]);
      if (val) { nm=val.name; if ((val=val.value)!="") {
        if (test.indexOf('isEmail')!=-1) { p=val.indexOf('@');
          if (p<1 || p==(val.length-1)) errors+='- '+nm+' must contain an e-mail address.\n';
        } else if (test!='R') { num = parseFloat(val);
          if (isNaN(val)) errors+='- '+nm+' must contain a number.\n';
          if (test.indexOf('inRange') != -1) { p=test.indexOf(':');
            min=test.substring(8,p); max=test.substring(p+1);
            if (num<min || max<num) errors+='- '+nm+' must contain a number between '+min+' and '+max+'.\n';
      } } } else if (test.charAt(0) == 'R') errors += '- '+nm+' is required.\n'; }
    } if (errors) alert('The following error(s) occurred:\n'+errors);
    document.MM_returnValue = (errors == '');
} }
</script>

<link href="../style/pro_drop_1.css" type="text/css" rel="stylesheet"/>
<script src="../style/stuHover.js" type="text/javascript"></script>
</head>

<body><center>
<table width="900" bgcolor="#FFFFFF">
	<?php
	include "topmenu_admin.php";
	?>
    <tr>
    	<td id="menu">
        <ul>
        	<li><a href="index.php">Home</a></li>
        	<li><a href="nilai.php">Daftar Kelas</a></li>
   	    <li><a href="jadwal.php">Jadwal</a></li>
        	<li><a href="passE.php">Ubah Password</a></li>
            <li><a href="../logout.php">Logout</a></li>
        </ul>
        </td>
    	<td id="konten">&nbsp;
          <form action="<?php echo $editFormAction; ?>" method="post" name="form1" id="form1" onsubmit="MM_validateForm('ul1','','R','ul2','','R','ul3','','R','ul4','','R','ul5','','R','tgs1','','R','tgs2','','R','tgs3','','R','tgs4','','R','tgs5','','R','uts','','R','uas','','R','nilai_akhir','','R');return document.MM_returnValue">
            <table align="center">
              
              <tr valign="baseline">
                <td nowrap="nowrap" align="right">Ulangan 1:</td>
                <td><input name="ul1" type="text" id="ul1" onchange="hitungNilai()" value="<?php echo htmlentities($row_nilaiE['ul1'], ENT_COMPAT, 'utf-8'); ?>" size="32" /></td>
              </tr>
              <tr valign="baseline">
                <td nowrap="nowrap" align="right">Ulangan 2:</td>
                <td><input name="ul2" type="text" id="ul2" onchange="hitungNilai()" value="<?php echo htmlentities($row_nilaiE['ul2'], ENT_COMPAT, 'utf-8'); ?>" size="32" /></td>
              </tr>
              <tr valign="baseline">
                <td nowrap="nowrap" align="right">Ulangan 3:</td>
                <td><input name="ul3" type="text" id="ul3" onchange="hitungNilai()" value="<?php echo htmlentities($row_nilaiE['ul3'], ENT_COMPAT, 'utf-8'); ?>" size="32" /></td>
              </tr>
              <tr valign="baseline">
                <td nowrap="nowrap" align="right">Ulangan 4:</td>
                <td><input name="ul4" type="text" id="ul4" onchange="hitungNilai()" value="<?php echo htmlentities($row_nilaiE['ul4'], ENT_COMPAT, 'utf-8'); ?>" size="32" /></td>
              </tr>
              <tr valign="baseline">
                <td nowrap="nowrap" align="right">Ulangan 5:</td>
                <td><input name="ul5" type="text" id="ul5" onchange="hitungNilai()" value="<?php echo htmlentities($row_nilaiE['ul5'], ENT_COMPAT, 'utf-8'); ?>" size="32" /></td>
              </tr>
              <tr valign="baseline">
                <td nowrap="nowrap" align="right">Tugas 1:</td>
                <td><input name="tgs1" type="text" id="tgs1" onchange="hitungNilai()" value="<?php echo htmlentities($row_nilaiE['tgs1'], ENT_COMPAT, 'utf-8'); ?>" size="32" /></td>
              </tr>
              <tr valign="baseline">
                <td nowrap="nowrap" align="right">Tugas 2:</td>
                <td><input name="tgs2" type="text" id="tgs2" onchange="hitungNilai()" value="<?php echo htmlentities($row_nilaiE['tgs2'], ENT_COMPAT, 'utf-8'); ?>" size="32" /></td>
              </tr>
              <tr valign="baseline">
                <td nowrap="nowrap" align="right">Tugas 3:</td>
                <td><input name="tgs3" type="text" id="tgs3" onchange="hitungNilai()" value="<?php echo htmlentities($row_nilaiE['tgs3'], ENT_COMPAT, 'utf-8'); ?>" size="32" /></td>
              </tr>
              <tr valign="baseline">
                <td nowrap="nowrap" align="right">Tugas 4:</td>
                <td><input name="tgs4" type="text" id="tgs4" onchange="hitungNilai()" value="<?php echo htmlentities($row_nilaiE['tgs4'], ENT_COMPAT, 'utf-8'); ?>" size="32" /></td>
              </tr>
              <tr valign="baseline">
                <td nowrap="nowrap" align="right">Tugas 5:</td>
                <td><input name="tgs5" type="text" id="tgs5" onchange="hitungNilai()" value="<?php echo htmlentities($row_nilaiE['tgs5'], ENT_COMPAT, 'utf-8'); ?>" size="32" /></td>
              </tr>
              <tr valign="baseline">
                <td nowrap="nowrap" align="right">UTS:</td>
                <td><input name="uts" type="text" id="uts" onchange="hitungNilai()" value="<?php echo htmlentities($row_nilaiE['uts'], ENT_COMPAT, 'utf-8'); ?>" size="32" /></td>
              </tr>
              <tr valign="baseline">
                <td nowrap="nowrap" align="right">UAS:</td>
                <td><input name="uas" type="text" id="uas" onchange="hitungNilai()" value="<?php echo htmlentities($row_nilaiE['uas'], ENT_COMPAT, 'utf-8'); ?>" size="32" /></td>
              </tr>
              <tr valign="baseline">
                <td nowrap="nowrap" align="right">Nilai Akhir:</td>
                <td><input name="nilai_akhir" type="text" id="nilai_akhir" value="<?php echo htmlentities($row_nilaiE['nilai_akhir'], ENT_COMPAT, 'utf-8'); ?>" size="32" /></td>
              </tr>
              <tr valign="baseline">
                <td nowrap="nowrap" align="right">&nbsp;</td>
                <td><input type="submit" value="Edit Nilai" /></td>
              </tr>
            </table>
            <input type="hidden" name="nis" value="<?php echo $row_nilaiE['nis']; ?>" />
            <input type="hidden" name="kd_kelas" value="<?php echo htmlentities($row_nilaiE['kd_kelas'], ENT_COMPAT, 'utf-8'); ?>" />
            <input type="hidden" name="kd_kat_kelas" value="<?php echo htmlentities($row_nilaiE['kd_kat_kelas'], ENT_COMPAT, 'utf-8'); ?>" />
            <input type="hidden" name="kd_matpel" value="<?php echo htmlentities($row_nilaiE['kd_matpel'], ENT_COMPAT, 'utf-8'); ?>" />
            <input type="hidden" name="tahun_ajaran" value="<?php echo htmlentities($row_nilaiE['tahun_ajaran'], ENT_COMPAT, 'utf-8'); ?>" />
            <input type="hidden" name="semester" value="<?php echo $row_nilaiE['semester']; ?>" />
            <input type="hidden" name="MM_update" value="form1" />
            <input type="hidden" name="nis" value="<?php echo $row_nilaiE['nis']; ?>" />
          </form>
      <p>&nbsp;</p></td>
    </tr>
    <tr>
    	<td colspan="2" id="footer">Copyright 2012</td>
    </tr>
    
</table></center>
</body>
</html>
<?php
mysql_free_result($nilaiE);
?>
